﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace TTCaller
{
    public partial class JobConfirmPanel : UserControl
    {

        public JobConfirmPanel()
        {
            InitializeComponent();
        }

        bool m_bIsEmpty = false;
        string m_strOrderID = "";
        int m_nPlaySound = 0;
        public void initial(string arg_strCmd)
        {
            this.button_ReDispatch.Visible = false;
            //JF0 107060112345601 0023005 0021010 2125015 S\r\n
            char cResult = arg_strCmd[1];
            m_strOrderID = arg_strCmd.Substring(3, 15);
            switch (cResult)
            {
                case 'F':
                    m_bIsEmpty = false;
                    setButton(arg_strCmd.Substring(18));
                    label_ResultTitle.Text = "派車結果：車足夠";
                    this.button_Confirm.Enabled = checkSelected();
                    break;
                case 'P':
                    m_bIsEmpty = false;
                    setButton(arg_strCmd.Substring(18));
                    label_ResultTitle.Text = "派車結果：車不夠";
                    this.button_Confirm.Enabled = checkSelected();
                    break;
                case 'N':
                    m_bIsEmpty = true;
                    label_ResultTitle.Text = "派車結果：沒有車";
                    this.button_ReDispatch.Visible = true;
                    this.button_Confirm.Text = "主畫面";                    
                    break;
            }

            this.timer_Tick.Enabled = true;
            SoundManager.getInstance().playJobGot();
            m_nPlaySound = 15;

            this.label_Hint.Text = "請同意或取消用車";

        }

        private void setButton(string arg_str)
        {
            this.label_Line1.Visible = false;
            this.button_Line1OK.Visible = false;
            this.button_Line1Cancel.Visible = false;
            this.label_Line1.Tag = "C";

            this.label_Line2.Visible = false;
            this.button_Line2OK.Visible = false;
            this.button_Line2Cancel.Visible = false;
            this.label_Line2.Tag = "C";

            this.label_Line3.Visible = false;
            this.button_Line3OK.Visible = false;
            this.button_Line3Cancel.Visible = false;
            this.label_Line3.Tag = "C";


            LogManager.getInstance().Log("JCP.SB:" + arg_str);
            string str = arg_str.Trim();
            str = str.Substring(0, str.Length - 1);

            LogManager.getInstance().Log("JCP.SB2:" + str);
            int i = 0;
            while (str.Length >= 7)
            {
                string strTemp = str.Substring(0, 7);
                switch (i)
                {
                    case 0:
                        this.label_Line1.Visible = true;
                        this.button_Line1OK.Visible = true;
                        this.button_Line1Cancel.Visible = true;
                        this.label_Line1.Text = "" + strTemp.Substring(0, 4) + "車 " + strTemp.Substring(4).TrimStart('0') + " 分鐘";
                        this.label_Line1.Tag = strTemp.Substring(0, 7);

                        break;

                    case 1:
                        this.label_Line2.Visible = true;
                        this.button_Line2OK.Visible = true;
                        this.button_Line2Cancel.Visible = true;
                        this.label_Line2.Text = "" + strTemp.Substring(0, 4) + "車 " + strTemp.Substring(4).TrimStart('0') + " 分鐘";
                        this.label_Line2.Tag = strTemp.Substring(0, 7);
                        break;

                    case 2:
                        this.label_Line3.Visible = true;
                        this.button_Line3OK.Visible = true;
                        this.button_Line3Cancel.Visible = true;
                        this.label_Line3.Text = "" + strTemp.Substring(0, 4) + "車 " + strTemp.Substring(4).TrimStart('0') + " 分鐘";
                        this.label_Line3.Tag = strTemp.Substring(0, 7);

                        break;
                }
                i += 1;
                str = str.Substring(7);
                LogManager.getInstance().Log("JCP.SB3:" + str);
            }


        }

        private void button_Line1OK_Click(object sender, EventArgs e)
        {
            m_nPlaySound = 0;
            Button b = (Button)sender;
            string str = System.Convert.ToString(b.Tag);
            if (str != null && str.Length == 2)
            {
                switch (str[0])
                {
                    case '1':
                        this.button_Line1OK.Enabled = true;
                        this.button_Line1Cancel.Enabled = true;
                        b.Enabled = false;
                        break;
                    case '2':
                        this.button_Line2OK.Enabled = true;
                        this.button_Line2Cancel.Enabled = true;
                        b.Enabled = false;
                        break;
                    case '3':
                        this.button_Line3OK.Enabled = true;
                        this.button_Line3Cancel.Enabled = true;
                        b.Enabled = false;
                        break;

                }

            }
            this.button_Confirm.Enabled = checkSelected();
            
        }

        private void button_ReDispatch_Click(object sender, EventArgs e)
        {
            m_nPlaySound = 0;
            string strResult = CmdTalker.getInstance().SendServerAndWaitResponse("CI1" + Form_Main.Main.m_strLastCallTaxiCount + "0" +m_strOrderID + StoreData.getInstance().TEL + "0", 5, "MOK", true);

            if (strResult == "MOK")
            {
                //wait result
            }
            else
            {
                //fail , re send
            }
        }

        private bool checkSelected()
        {
            bool bResult = true;
            if (this.button_Line1OK.Visible)
            {
                if (this.button_Line1OK.Enabled && this.button_Line1Cancel.Enabled)
                    bResult = false;
            }
            if (this.button_Line2OK.Visible)
            {
                if (this.button_Line2OK.Enabled && this.button_Line2Cancel.Enabled)
                    bResult = false;
            }
            if (this.button_Line3OK.Visible)
            {
                if (this.button_Line3OK.Enabled && this.button_Line3Cancel.Enabled)
                    bResult = false;
            }
            return bResult;

        }

        private void button_Confirm_Click(object sender, EventArgs e)
        {
            m_nPlaySound = 0;
            if (this.button_Confirm.Text == "主畫面")
            {
                Form_Main.Main.ChangeToMainScreen();
            }
            else
            {
                if (!checkSelected())
                    return;

                string strTemp = "J" + m_strOrderID;

                //Y的部份, 要新增叫車記錄

                if (button_Line1OK.Visible)
                {
                    if (button_Line1OK.Enabled)
                    {
                        strTemp += "N" + getDispatchCallNo(this.label_Line1.Tag);
                    }
                    else
                    {
                        strTemp += "Y" + getDispatchCallNo(this.label_Line1.Tag);
                        addDispatchRec(m_strOrderID, this.label_Line1.Tag);
                    }
                }
                if (button_Line2OK.Visible)
                {
                    if (button_Line2OK.Enabled)
                    {
                        strTemp += "N" + getDispatchCallNo(this.label_Line2.Tag);
                    }
                    else
                    {
                        strTemp += "Y" + getDispatchCallNo(this.label_Line2.Tag);
                        addDispatchRec(m_strOrderID, this.label_Line2.Tag);
                    }
                }
                if (button_Line3OK.Visible)
                {
                    if (button_Line3OK.Enabled)
                    {
                        strTemp += "N" + getDispatchCallNo(this.label_Line3.Tag);
                    }
                    else
                    {
                        strTemp += "Y" + getDispatchCallNo(this.label_Line3.Tag);
                        addDispatchRec(m_strOrderID, this.label_Line3.Tag);
                    }
                }

                string strResult = CmdTalker.getInstance().SendServerAndWaitResponse(strTemp, 5, "MOK", true);

                if (strResult == "MOK")
                {
                    //wait result
                    Form_Main.Main.ChangeToMainScreen();
                }
                else
                {
                    //fail , re send
                    LogManager.getInstance().Log("Send Fail:"+ strTemp, false);
                }

            }

        }

        public string getDispatchCallNo(object arg_strTag)
        {
            //0001005

            try
            {
                return System.Convert.ToString(arg_strTag).Substring(0, 4);
            }
            catch (Exception ex)
            {
            }
            return "XXXX";

        }

        public void addDispatchRec(string arg_strOrderID, object arg_strCallNo)
        {
            StoreData.getInstance().addDispatch("JA0" + arg_strOrderID + arg_strCallNo + "ND");

        }

        private void timer_Tick_Tick(object sender, EventArgs e)
        {
            if (!this.Visible)
                return;
            if (m_nPlaySound > 0)
            {
                SoundManager.getInstance().playJobGot();
                m_nPlaySound -= 1;
            }
        }


    }
}
